package com.robot.base.util.Permission;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ColorUtils;
import com.hjq.permissions.IPermissionInterceptor;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.PermissionFragment;
import com.hjq.permissions.XXPermissions;
import com.robot.base.R;
import com.robot.base.base.callback.RobotCallBackBoolean;
import com.robot.base.view.CommonDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/XXPermissions
 * time   : 2021/01/04
 * desc   : 权限申请拦截器
 */
public final class PermissionInterceptor implements IPermissionInterceptor {

    @Override
    public void launchPermissionRequest(@NonNull Activity activity, @NonNull List<String> allPermissions, @Nullable OnPermissionCallback callback) {
        List<String> deniedPermissions = XXPermissions.getDenied(activity, allPermissions);
        String permissionString = PermissionNameConvert.getPermissionString(activity, deniedPermissions);
//        String permissionTitle = PermissionNameConvert.getPermissionTitleString(activity, deniedPermissions);
        // 这里的 Dialog 只是示例，没有用 DialogFragment 来处理 Dialog 生命周期
        new CommonDialog.Builder(activity)
//                .setTitle(permissionTitle)
                .setDes(permissionString)
                .setDesColor(ColorUtils.getColor(R.color.color33))
                .setPositiveButton(
                        "确认",
                        ColorUtils.getColor(R.color.color33),
                        new RobotCallBackBoolean() {
                            @Override
                            public void action(int type, CommonDialog dialog) {
                                dialog.dismiss();
                                PermissionFragment.launch(activity, new ArrayList<>(allPermissions), PermissionInterceptor.this, callback);
                            }
                        })
                .setNegativeButton(
                        "取消",
                        ColorUtils.getColor(R.color.color33),
                        new RobotCallBackBoolean() {
                            @Override
                            public void action(int type, CommonDialog dialog) {
                                dialog.dismiss();
                                if (callback != null) {
                                    callback.onDenied(deniedPermissions, false);
                                }
                            }
                        })
                .create().show();
    }
}